package com.example.weibo_suhuanzhen.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.weibo_suhuanzhen.activities.DetailActivity;
import com.example.weibo_suhuanzhen.R;
import com.example.weibo_suhuanzhen.bean.WeiboInfo;

import java.util.ArrayList;
import java.util.List;

public class ImageGridAdapter extends RecyclerView.Adapter<ImageGridAdapter.GridImageViewHolder> {
    private Context context;
    private List<String> imageUrls;
    private WeiboInfo item;

    public ImageGridAdapter(Context context, List<String> imageUrls, WeiboInfo item) {
        this.context = context;
        this.imageUrls = imageUrls;
        this.item = item;
    }

    @NonNull
    @Override
    public GridImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_grid_image, parent, false);
        return new GridImageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GridImageViewHolder holder, int position) {
        String imageUrl = imageUrls.get(position);
        Glide.with(context).load(imageUrl).into(holder.imageView);
        holder.imageView.setOnClickListener(v -> {
            Intent intent = new Intent(context, DetailActivity.class);
            intent.putExtra("username", item.getUsername());
            intent.putExtra("avatar", item.getAvatar());
            intent.putExtra("position",position);
            intent.putStringArrayListExtra("images", new ArrayList<>(imageUrls));
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return imageUrls.size();
    }

    public static class GridImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public GridImageViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.grid_image_view);
        }
    }
}
